﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor.Controls
{
    public partial class KeyFrameControl : UserControl
    {
        EditorState ES;
        System.Threading.Timer Timer;
        Jemgine.Animation.AnimationPlayer AnimationPlayer;
        Jemgine.Data.Animation PlayingAnimation = null;

        internal void SetEditorState(EditorState ES)
        {
            this.ES = ES;
            ES.OnSelection += OnSelectionChanged;
            
        }


        public KeyFrameControl()
        {
            InitializeComponent();
            AnimationPlayer = new Animation.AnimationPlayer();
            Timer = new System.Threading.Timer((A) => UpdatePlayer(), null, TimeSpan.FromSeconds(0),
                TimeSpan.FromSeconds(1.0f / 10.0f));
        }

        DateTime LastDraw = DateTime.Now;

        private void UpdatePlayer()
        {
            //var Now = DateTime.Now;
            //var ElapsedTime = Now - LastDraw;
            //LastDraw = Now;

            //AnimationPlayer.Update((float)ElapsedTime.TotalSeconds);

            //if (Selected != null && ES != null)
            //{
            //    if (PlayingAnimation != null)
            //        SetAnimationTransforms();
            //    else
            //        Selected.SetTransform(Selected.GetFrameAngles(frameList1.SelectedItem));
            //    ES.OnInvalidateMap();
            //}
        }

        private void SetAnimationTransforms()
        {
            float FrameTime = AnimationPlayer.GetFloatTime();
            int StartFrame = (int)System.Math.Floor(FrameTime);
            int EndFrame = StartFrame + 1;
            if (EndFrame >= PlayingAnimation.Frames.Count) EndFrame = 0;
            float Percentage = FrameTime - (float)StartFrame;
            Selected.SetTransform(Data.Skeleton.BlendFrameAngles(
                Selected.GetFrameAngles(PlayingAnimation.Frames[StartFrame]),
                Selected.GetFrameAngles(PlayingAnimation.Frames[EndFrame]),
                Percentage));
        }

        Data.Skeleton Selected = null;
        internal void OnSelectionChanged()
        {
            Selected = ES.GetMiddleObject(ES.Selected) as Data.Skeleton;
            if (Selected == null)
            {
                this.Enabled = false;
                frameList1.ItemCount = 0;
                frameList1.SelectedItem = -1;
                frameList1.Hilites.Clear();
                listView1.Clear();
            }
            else
            {
                this.Enabled = true;
                frameList1.ItemCount = Selected.KeyFrames.Height;
                frameList1.SelectedItem = Selected.EditorFrame;
                BuildAnimationList();
                SetHilites(0);
            }

            animProperties.SelectedObject = null;
            Invalidate();
        }

        private void SetHilites(int Index)
        {
            frameList1.Hilites.Clear();
            frameList1.Hilites.Add(new FrameList.ItemHilite { Index = Index - 1, Color = Brushes.LightGreen });
            frameList1.Hilites.Add(new FrameList.ItemHilite { Index = Index, Color = Brushes.Purple });
            frameList1.Hilites.Add(new FrameList.ItemHilite { Index = Index + 1, Color = Brushes.LightBlue });
        }

        private void BuildAnimationList()
        {
            listView1.Clear();
            foreach (var Anim in Selected.Animations)
            {
                ListViewItem Item = new ListViewItem(Anim.Name);
                Item.Tag = Anim;
                listView1.Items.Add(Item);
            }
        }

        private void frameList1_FramePicked(object sender, FrameList.FramePickedEventArgs e)
        {
            //if (Selected != null) Selected.SelectedKeyFrame = frameList1.SelectedItem;
            SetHilites(frameList1.SelectedItem);
            if (Selected != null) Selected.EditorFrame = frameList1.SelectedItem;
        }

        private void _addKeyframe_Click(object sender, EventArgs e)
        {
            if (Selected != null)
            {
                Selected.KeyFrames.AddRow(frameList1.SelectedItem + 1);
                //Selected.RootBone.InsertKeyframe(Selected.SelectedKeyFrame + 1);
                frameList1.ItemCount = Selected.KeyFrames.Height;
            }
        }

        private void NewAnimButton_Click(object sender, EventArgs e)
        {
            if (Selected != null)
            {
                Selected.Animations.Add(new Data.Animation());
                BuildAnimationList();
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            animProperties.SelectedObject = null;
            if (listView1.SelectedItems.Count > 0)
                animProperties.SelectedObject = listView1.SelectedItems[0].Tag;
        }

        private void playPauseButton_Click(object sender, EventArgs e)
        {
            if (PlayingAnimation != null)
            {
                PlayingAnimation = null;
                return;
            }

            if (listView1.SelectedItems.Count != 1) return;

            PlayingAnimation = listView1.SelectedItems[0].Tag as Data.Animation;
            AnimationPlayer.Play(PlayingAnimation.Rate, PlayingAnimation.Frames.Count, true);
        }
    }
}
